<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        div {
            width: 300px;
            height: 300px;
            background: pink;
            margin: 100px;
            cursor: pointer;
        }
    </style>
</head>

<body>
    <div></div>
    <script>
        var div = document.querySelector('div');
        div.addEventListener('mousemove', function(e) {
            // 获取鼠标在页面中的坐标
            // var pagex = e.pageX;
            // var pagey = e.pageY;
            //获取盒子的距离
            // var left = div.offsetLeft;
            // var top = div.offsetTop;
            //鼠标在盒子里的坐标
            // var x = pagex - left;
            // var y = pagey - top;
            var x = e.pageX - div.offsetLeft;
            var y = e.pageY - div.offsetTop;
            div.innerHTML = 'x坐标是' + x + 'px,' + 'y坐标是' + y + 'px';

        })
    </script>

</body>

</html>